Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): bump messageformat parser. Add TS types #1328

Merged

Conversation

timofei-iatsenko
Copy link
Collaborator

@timofei-iatsenko timofei-iatsenko commented Jan 9, 2023

Bumped version of the upstream messageformat parser to 5.0, updated what's broke and add TS typings wherever possible in touched files.

Resolves
#1278
#1303

Should be no changes in logic, but public types slightly changed. They became more accurate, so it's better to bump a version just in case.

How current config of semver works, should i name a PR with special prefix or commit message or it's doing manually?

@vercel
Copy link

vercel bot commented Jan 9, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
js-lingui ✅ Ready (Inspect) Visit Preview Jan 11, 2023 at 8:47AM (UTC)

@andrii-bodnar
Copy link
Contributor

@thekip thanks for the contribution!

Probably, I'll ask @Martin005 to review this PR.

How current config of semver works, should i name a PR with special prefix or commit message or it's doing manually?

It expects that the PR title will be specified according to the following format:

https://github.com/amannn/action-semantic-pull-request#validation

@Martin005
Copy link
Contributor

Martin005 commented Jan 10, 2023

@thekip You made changes only to the @lingui/core package.
Could you please also update the @lingui/cli and @lingui/remote-loader? They currently also use the messageformat-parser package.

Furthermore, updating the package may also fix issues #1033 and #1075 🙂

@github-actions
Copy link

github-actions bot commented Jan 10, 2023

size-limit report 📦

Path Size
./packages/core/build/cjs/core.production.min.js 2.9 KB (0%)
./packages/detect-locale/build/cjs/detect-locale.production.min.js 798 B (0%)
./packages/react/build/cjs/react.production.min.js 4.91 KB (0%)
./packages/remote-loader/build/cjs/remote-loader.production.min.js 92.08 KB (0%)

@timofei-iatsenko
Copy link
Collaborator Author

My bad, didn't check this.

This commit is bigger, few things about what changed:

  1. I've found a code duplication (3 times) where tokens from message-parser were converted to Lingui internal format.
    I removed this code duplication by exporting from @lingui/core a parseMessage function and reusing it in all subpackages.

  2. I simplified the way how the compiled catalogs produced. This was for 3 purposes:

    1. to reuse mentioned function
    2. to make code much cleaner and smaller
    3. to make reading of catalogs actually faster than before.

Before compiled catalogs were produced as js template literal (js code) and complicated AST manipulation was involved

export const messages = {
  messageId: "message value"
}

Now i'm just using JSON.stringify / JSON.parse

export const messages = JSON.parse("{
 "messageId": "message value"
}")

Why it's better you can read here https://v8.dev/blog/cost-of-javascript-2019#json

@Martin005 Martin005 changed the title refactor(core) bump messageformat parser. Add TS types refactor(core): bump messageformat parser. Add TS types Jan 10, 2023
Copy link
Contributor

@Martin005 Martin005 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the changes, I think that all of what you mentioned makes perfect sense including the JSON.stringify/JSON.parse! 🙂

When I tried your branch as a local package, I immediately noticed one mistake, so please have a look at my comments and resolve them.

When I resolved the mistake locally, I also tried all 4 issues #1033, #1075, #1278 and #1303 and I can confirm that all of them are fixed with this new version of @messageformat/parser package 🥳

@@ -28,6 +28,7 @@ function remoteLoader<T>({ format = "minimal", fallbackMessages, messages}: Remo
`)
}

// todo: that will not work with context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this todo

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This todo is here because the code in codebase already not working with context and changing this package was out of scope of that PR.

packages/core/src/i18n.ts Outdated Show resolved Hide resolved
@timofei-iatsenko
Copy link
Collaborator Author

Fixed

@timofei-iatsenko
Copy link
Collaborator Author

I also thinking on how to hide exposed but treated as internal APIs, for example that formatMessage is supposed to be internal api, but because we use it in other packages it should be exported.

There should be either special naming convention, or may be just JSdoc would be enough

@timofei-iatsenko
Copy link
Collaborator Author

I see tests are failing, i will have a look

@timofei-iatsenko
Copy link
Collaborator Author

I've ported some changes from this PR #1328

I deleted pure option from createCompiledCatalog because it's not used anywhere in the project. This API considered internal, so we should be on the safe side.
It's probably was created for remote-loader but code has been changed since then and now this option is obsolete.

And also i've ported tests, because previous ones didn't test anything (there was a logical issue)

Copy link
Contributor

@Martin005 Martin005 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you for the changes!

@andrii-bodnar andrii-bodnar merged commit 5aa65d4 into lingui:main Jan 11, 2023
@timofei-iatsenko timofei-iatsenko deleted the fix/bump-messageformat-parser branch January 11, 2023 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants